package com.ruoyi.project.dict.ebookcatalog.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.ebookcatalog.domain.EbookCatalog;
import com.ruoyi.project.dict.ebookcatalog.service.IEbookCatalogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电子书目录Controller
 *
 * @author shixiangwen
 * @date 2021-12-29
 */
@Controller
@RequestMapping("/dict/ebookcatalog")
public class EbookCatalogController extends BaseController {
    private String prefix = "dict/ebookcatalog";

    @Autowired
    private IEbookCatalogService ebookCatalogService;

    @RequiresPermissions("dict:ebookcatalog:view")
    @GetMapping("/{ebookId}")
    public String ebookcatalog(@PathVariable("ebookId") Integer ebookId, ModelMap mmap) {
        mmap.put("ebookId", ebookId);
        return prefix + "/ebookcatalog";
    }

    /**
     * 查询电子书目录列表
     */
    @RequiresPermissions("dict:ebookcatalog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EbookCatalog ebookCatalog) {
        startPage();
        List<EbookCatalog> list = ebookCatalogService.selectEbookCatalogList(ebookCatalog);
        return getDataTable(list);
    }

    /**
     * 导出电子书目录列表
     */
    @RequiresPermissions("dict:ebookcatalog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EbookCatalog ebookCatalog) {
        List<EbookCatalog> list = ebookCatalogService.selectEbookCatalogList(ebookCatalog);
        ExcelUtil<EbookCatalog> util = new ExcelUtil<EbookCatalog>(EbookCatalog.class);
        return util.exportExcel(list, "ebookcatalog");
    }

    /**
     * 新增电子书目录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存电子书目录
     */
    @RequiresPermissions("dict:ebookcatalog:add")
    @Log(title = "电子书目录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EbookCatalog ebookCatalog) {
        return toAjax(ebookCatalogService.insertEbookCatalog(ebookCatalog));
    }

    /**
     * 修改电子书目录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        EbookCatalog ebookCatalog = ebookCatalogService.selectEbookCatalogById(id);
        mmap.put("ebookCatalog", ebookCatalog);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子书目录
     */
    @RequiresPermissions("dict:ebookcatalog:edit")
    @Log(title = "电子书目录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EbookCatalog ebookCatalog) {
        return toAjax(ebookCatalogService.updateEbookCatalog(ebookCatalog));
    }

    /**
     * 删除电子书目录
     */
    @RequiresPermissions("dict:ebookcatalog:remove")
    @Log(title = "电子书目录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ebookCatalogService.deleteEbookCatalogByIds(ids));
    }
}
